package com.chinasofti.field;

import java.lang.reflect.Field;

public class TestFieldDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException {
        //method1();
        Class clazz = Class.forName("com.chinasofti.ref.field.Student");
        Field field = clazz.getDeclaredField("money");
        //取消访问检查
        field.setAccessible(true);

        Student student = (Student) clazz.newInstance();
        //获取money的值
        Object o = field.get(student);

        System.out.println(o);
    }

    private static void method1() throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        Class clazz = Class.forName("com.chinasofti.ref.field.Student");

        Field field = clazz.getField("name");
        Student student = (Student)clazz.newInstance();
        //使用set(obj,value)进行赋值: 给objct对象成员变量赋值为value
        field.set(student,"zhangsan");

        System.out.println("student = " + student);
    }

}